/*
********************************************************************************
* Copyright (C) 2022, xiang.D <dx_65535@163.com>.
* All right reserved.
*
* File Name   : safe_manager.cc
* Author      :
* Version     : V1.0
* Description :
* Journal     : 2022-03-06 version v1.0
* Brief       : Blog: https://blog.csdn.net/qq_38750572?spm=1001.2014.3001.5343
* Others      :
                Compile command: make
********************************************************************************
*/

#include <stdio.h>
#include "safe_manager.h"

using namespace std;

#define MGR_LOGD(fmt, args...) printf("%d SaveMgr D: " fmt, __LINE__, ##args)
#define MGR_LOGW(fmt, args...) printf("%d SaveMgr W: " fmt, __LINE__, ##args)
#define MGR_LOGE(fmt, args...) printf("%d SaveMgr E: " fmt, __LINE__, ##args)
#define MGR_LOG(fmt, args...)  printf(fmt, ##args)

CSafeMgr::CSafeMgr()
{
    mPartArray.clear();
    mpGpu = new CPartGpu("gpu", 0, 1);
    mpSoundCard = new CPartSoundCard("sound card", 1, 1);

    AddPartArray(mpGpu);
    AddPartArray(mpSoundCard);
}

CSafeMgr::~CSafeMgr()
{
    for (auto it = mPartArray.begin(); it != mPartArray.end(); ++it) {
        if (nullptr != (*it)) {
            delete (*it);
        }
    }
}

void CSafeMgr::AddPartArray(CComputePartBase *pPart)
{
    if (NULL == pPart) {
        MGR_LOGE("pPart is NULL!\n");
        return ;
    }

    mPartArray.push_back(pPart);
}

void CSafeMgr::Accept(CVisitorBase *visitor)
{
    vector<CComputePartBase *>::iterator it;

    for (it = mPartArray.begin(); it != mPartArray.end(); ++it) {
        (*it)->Accept(visitor);
    }
}

void CSafeMgr::CheckItem(ECheckItem item)
{
    switch (item)
    {
        case ITEM_DRIVER:
            Accept(&theDriverCheck);
        break;

        case ITEM_FUNCTION:
            Accept(&theFunctionCheck);
        break;

        case ITEM_ALL:
            Accept(&theDriverCheck);
            Accept(&theFunctionCheck);
        break;

        default:
            MGR_LOGW("No this item!\n");
        break;
    } 
}

